﻿using AutoMapper;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;

namespace Zocono.WCS.Domain.TelegramRecordInfoDomain
{
    public class TelegramRecordInfoService : ITelegramRecordInfoService
    {
        private readonly ILogUtil _LogUtil;
        private readonly ISnowflake _Snowflake;
        private readonly ITelegramRecordInfoRepository _TelegramRecordInfoRepository;
        private readonly IMapper _Mapper;

        public TelegramRecordInfoService(ILogUtil logUtil
            , ISnowflake snowflake
            , ITelegramRecordInfoRepository telegramRecordInfoRepository
            , IMapper mapper)
        {
            _LogUtil = logUtil;
            _Snowflake = snowflake;
            _TelegramRecordInfoRepository = telegramRecordInfoRepository;
            _Mapper = mapper;
        }

        public async Task<bool> BatchInsertAsync(List<TelegramRecordInfo> telegramRecordList)
        {
            telegramRecordList.ForEach(c => c.ID = _Snowflake.GetId());

            var isOk = await _TelegramRecordInfoRepository.InsertRangeAsync(telegramRecordList);
            return isOk;
        }

        public async Task<bool> CreateTelegramRecordInfoAsync(TelegramRecordInfo telegramRecord)
        {
            telegramRecord.ID = _Snowflake.GetId();

            var isOk = await _TelegramRecordInfoRepository.InsertAsync(telegramRecord);

            return isOk;
        }

        /// <summary>
        /// 获取报文记录列表
        /// </summary>
        /// <param name="queryParams">查询条件</param>
        /// <returns></returns>
        public async Task<PageDto<TelegramRecordInfoDto>> GetPagedTelegramRecordInfoAsync(QueryTelegramRecordInfo queryParams)
        {
            PageModel page = new PageModel { PageIndex = queryParams.PageIndex, PageSize = queryParams.PageSize, };
            var equipmentCode = queryParams.EquipmentCode;
            var exp = Expressionable.Create<TelegramRecordInfo>();
            exp.AndIF(!string.IsNullOrEmpty(equipmentCode), (a) => a.EquipmentCode.ToString() == equipmentCode);
            var PageList = await _TelegramRecordInfoRepository.GetPagedTelegramRecordInfoAsync(exp.ToExpression(), page);

            if (PageList != null && PageList.Result.Count > 0)
            {
                foreach (var item in PageList.Result)
                {
                    item.TelegramUniqueID= JsonConvert.DeserializeObject<Dictionary<string,object>>(item.TelegramUniqueIDJson);
                    item.PlcTelegram = JsonConvert.DeserializeObject<Dictionary<string, object>>(item.PlcTelegramJson);
                    item.WcsTelegram = JsonConvert.DeserializeObject<Dictionary<string, object>>(item.WcsTelegramJson);
                }
            }

            return PageList;
        }
    }
}
